export function formatDateTime(timeStr) {
	if (!timeStr) return '';
	return timeStr.replace('T', ' ').split('.')[0];
}
export function getFormatDateTime(e) {
	const date = new Date(e) || new Date();
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，并且保证是两位数  
	const day = String(date.getDate()).padStart(2, '0'); // 保证日期是两位数  
	const hours = String(date.getHours()).padStart(2, '0'); // 保证小时是两位数  
	var time = `${year}-${month}-${day}`;
	return time
}
export function getTimeStamp(e) {
	var date = new Date(e);
	date.setDate(date.getDate());
	return date;
}
export function getFormatDate(date) {
	if (!date) date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minute >= 0 && minute <= 9) {
		minute = "0" + minute;
	}
	if (second >= 0 && second <= 9) {
		second = "0" + second;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
		" " + hour + seperator2 + minute +
		seperator2 + second;
	return currentdate;
}


export function getBefore7DateTime(inputDate) {
	const baseDate = inputDate ? new Date(inputDate) : new Date();
	const targetDate = new Date(baseDate);
	targetDate.setDate(targetDate.getDate() - 7); // 修正为7天前
	const year = targetDate.getFullYear();
	const month = String(targetDate.getMonth() + 1).padStart(2, '0');
	const day = String(targetDate.getDate()).padStart(2, '0');
	return `${year}-${month}-${day}`;
}


export function getchangeLevel(data) {
	// uni.showLoading({
	// 	title: '跳转页面中...',
	// 	mask: true
	// })
	uni.redirectTo({
		url: data.url || '/st_mes_zs/index/index',
		animationType: 'none'
	});
}




export const navigateMixin = {
	methods: {
		/**
		 * 通用导航方法
		 * @param {Object} options 配置参数
		 * @param {string} options.modulePath Vuex模块路径（如：'GuyueModule'）
		 * @param {string} options.targetUrl 目标页面URL
		 * @param {Object} options.payload 需要携带的数据对象
		 * @param {Object} [options.animation] 动画配置
		 * @param {number} [options.disableDuration=2000] 禁用时长（毫秒）
		 */
		async $safeNavigate(options) {
			const {
				modulePath = 'GuyueModule',
					targetUrl,
					payload,
					// animation = {
					// 	type: 'slide-in-right',
					// 	duration: 100
					// },
					animation = {
						type: 'none',
					},
					disableDuration = 2000
			} = options;

			if (this.disabled) return;

			try {
				// 禁用交互
				this.disabled = true;

				// 创建深拷贝方法
				const deepClone = data => JSON.parse(JSON.stringify(data));

				// 更新 ModuleData_select
				const newData = [
					...deepClone(this.$store.state[modulePath].ModuleData_select),
					payload
				];

				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'ModuleData_select',
					data: newData
				});

				// 更新 MesBasicInfo
				const newEquipment = {
					...deepClone(this.$store.state[modulePath].MesBasicInfo),
					ModuleData_select: newData
				};

				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfo',
					data: newEquipment
				});

				// 更新 MesBasicInfoList
				const updatedList = this.$store.state[modulePath].MesBasicInfoList.map(
					item => item.warehouse.EquipmentID === newEquipment.warehouse.EquipmentID ?
					newEquipment :
					item
				);

				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfoList',
					data: updatedList
				});
				// uni.showLoading({
				// 	title: '跳转页面中...',
				// 	mask: true
				// })
				uni.redirectTo({
					url: targetUrl,
				});

			} finally {
				// 恢复交互
				setTimeout(() => {
					this.disabled = false;
				}, disableDuration);
			}
		},


		async $safeStatus(options) {
			const {
				modulePath = 'GuyueModule',
					payload,
			} = options;
			try {
				// 创建深拷贝方法
				const deepClone = data => JSON.parse(JSON.stringify(data));
				const baseData = deepClone(this.$store.state[modulePath].MesBasicInfo);

				// 更新 MesBasicInfo
				const newEquipment = {
					...baseData,
					Status: {
						...(baseData.Status || {}), // 若原 Status 存在则展开，否则初始化为空对象
						ClockStatusTitle: payload // 替换为需要设置的具体值（如字符串、变量等）
					}
				};

				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfo',
					data: newEquipment
				});

				// 更新 MesBasicInfoList
				const updatedList = this.$store.state[modulePath].MesBasicInfoList.map(
					item => item.warehouse.EquipmentID === newEquipment.warehouse.EquipmentID ?
					newEquipment : item
				);
				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfoList',
					data: updatedList
				});
			} finally {

			}
		},


		async $modifyParentData(options) {
			const {
				modulePath = 'GuyueModule',
					payload,
			} = options;
			try {
				
				// 创建深拷贝方法
				const deepClone = data => JSON.parse(JSON.stringify(data));
				const ModuleData_selectData = deepClone(this.$store.state[modulePath].ModuleData_select);
				const baseData = deepClone(this.$store.state[modulePath].MesBasicInfo);

				
				
				if (ModuleData_selectData && ModuleData_selectData.length > 0) {
					ModuleData_selectData[ModuleData_selectData.length - 1] = options;
				}
				if (baseData.ModuleData_select && baseData.ModuleData_select.length > 0) {
					baseData.ModuleData_select[baseData.ModuleData_select.length - 1] = options;
				}
				
				

				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'ModuleData_select',
					data: ModuleData_selectData
				});
				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfo',
					data: baseData
				});

				// 更新 MesBasicInfoList
				const updatedList = this.$store.state[modulePath].MesBasicInfoList.map(
					item => item.warehouse.EquipmentID === baseData.warehouse.EquipmentID ?
					baseData : item
				);
				await this.$store.commit(`${modulePath}/ChangeAndSetStateData`, {
					Name1: 'MesBasicInfoList',
					data: updatedList
				});
			} finally {

			}
		}



	}
};




export const baseAddModuleData_select = {
	methods: {
		GetModuleData_select(res) {
			const routes = getCurrentPages();
			console.log(routes.length);

			if (this.$refs.framework?.ChangeTabsClick instanceof Function) {
				this.$refs.framework.ChangeTabsClick()
			}
			const newData = JSON.parse(JSON.stringify(this.$store.state.GuyueModule.MesBasicInfo
				.ModuleData_select));
			const lastItem = newData[newData.length - 1];
			let backIndex = routes.length - lastItem.length; // 计算返回页面栈数
			console.log(lastItem);



			if (lastItem && lastItem.nood && lastItem.nood.length > 0) {
				this.GolastItemUrl(lastItem)
			} else if (lastItem && lastItem.path) {
				this.GolastItemUrl(lastItem)
			} else if (lastItem && lastItem.nood.length == 0) {
				uni.showLoading({
					title: '跳转页面中...',
					mask: true
				})
				uni.redirectTo({
					url: lastItem.url || '/st_mes_zs/index/index'
				});
				this.moduleData_show = this.$store.state.GuyueModule.moduleData_basic;
			}
		},

		GolastItemUrl(lastItem, moduleData) {
			if (lastItem) {
				uni.showLoading({
					title: '跳转页面中...',
					mask: true
				})
				uni.redirectTo({
					url: lastItem.url || '/st_mes_zs/index/index'
				});
			}
		}
	}
}